﻿using DevExpressWebApplication1.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DevExpressWebApplication1.Module.CM
{
    public partial class Supplier : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void gvSupplier_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            int supplierId = int.Parse(hdfSupplierID.Get("SupId").ToString());
            if (supplierId > 0)
            {
                try
                {
                    SupplierUtils.Update(supplierId.ToString(), txtSupplier.Text, txtCompany.Text, txtPhone.Text, ControlUtils.GetDateEditValue(dtpDOB), txtAddress.Text, ControlUtils.GetCBBValue(cbbGroup));
                    gvSupplier.DataBind();
                }
                catch (Exception ex)
                {
                    Console.Write("Update Customer fails : " + ex.Message);
                }
            }
            else
            {
                try
                {
                    SupplierUtils.Insert(txtSupplier.Text, txtCompany.Text, txtPhone.Text, ControlUtils.GetDateEditValue(dtpDOB), txtAddress.Text, ControlUtils.GetCBBValue(cbbGroup));
                    gvSupplier.DataBind();
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                }
            }
        }

        protected void cbAddEdit_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            string[] split = e.Parameter.Split(';');
            if (split[0] == "Edit")
            {
                txtSupplier.Text = gvSupplier.GetRowValues(int.Parse(split[1].ToString()), "supplier_name").ToString();
                txtCompany.Text = gvSupplier.GetRowValues(int.Parse(split[1].ToString()), "company").ToString();
                txtPhone.Text = gvSupplier.GetRowValues(int.Parse(split[1].ToString()), "phone").ToString();
                ControlUtils.SetDateEditValue(dtpDOB, gvSupplier.GetRowValues(int.Parse(split[1].ToString()), "dob").ToString());
                txtAddress.Text = gvSupplier.GetRowValues(int.Parse(split[1].ToString()), "address").ToString();
                ControlUtils.SetCBBValue(cbbGroup, gvSupplier.GetRowValues(int.Parse(split[1].ToString()), "group_id").ToString());
                hdfSupplierID.Set("SupId", gvSupplier.GetRowValues(int.Parse(split[1].ToString()), "supplier_id").ToString());
            }
        }
    }
}